﻿using System;
using System.Threading;
using SimpleServiceBus.Utilities;

namespace SimpleServiceBus.Tests.Stubs
{
    public class TestTimerService : TimerServiceBase

    {
        public long Counter;
        public AutoResetEvent Wait = new AutoResetEvent(false);

        public TestTimerService(TimeSpan timerInterval) : base(timerInterval)
        {
        }

        public TimeSpan SleepDuration { get; set; }

        public DateTime LastTime { get; set; }
        public TimeSpan Duration { get; set; }

        protected override void StartService()
        {
            Counter = 0;
            base.StartService();
            LastTime = DateTime.Now;
        }

        protected override void PerformTimerAction()
        {
            Interlocked.Increment(ref Counter);
            Duration = DateTime.Now.Subtract(LastTime);
            LastTime = DateTime.Now;
            if (SleepDuration != TimeSpan.Zero)
            {
                Thread.Sleep(SleepDuration);
            }
            Wait.Set();
        }
    }
}